home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Text / SimpleText Sample / MovieFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-12  |  7.6 KB  |  257 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        MovieFile.c
  3.  
  4.     Contains:    Movie file support for simple text application
  5.  
  6.     Version:    SimpleText 1.4 or later
  7.  
  8. ** Copyright 1993-1996 Apple Computer. All rights reserved.
  9. **
  10. **    You may incorporate this sample code into your applications without
  11. **    restriction, though the sample code has been provided "AS IS" and the
  12. **    responsibility for its operation is 100% yours.  However, what you are
  13. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  14. **    after having made changes. If you're going to re-distribute the source,
  15. **    we require that you make it clear in the source that the code was
  16. **    descended from Apple Sample Code, but that you've made changes.
  17.  
  18. */
  19.  
  20. #include "MacIncludes.h"
  21.  
  22. #include "MovieFile.h"
  23.  
  24. #pragma segment MovieFile
  25.  
  26. // --------------------------------------------------------------------------------------------------------------
  27. static void DoFindInMovie(Movie theMovie, long *searchOffset, Boolean goBackwards)
  28. {
  29.     Track    searchTrack = nil;
  30.     OSErr    anErr;
  31.     
  32.     if (goBackwards)
  33.         (*searchOffset)--;
  34.     else
  35.         (*searchOffset)++;
  36.  
  37.     anErr = MovieSearchText(theMovie, (Ptr)&gFindString[1], gFindString[0],
  38.                 (gWrapAround ? findTextWrapAround : 0) |
  39.                 (gCaseSensitive ? findTextCaseSensitive : 0) |
  40.                 findTextUseOffset |
  41.                 (goBackwards ? findTextReverseSearch : 0),
  42.                 &searchTrack, (TimeValue*)nil, searchOffset);
  43.                 
  44.     if (anErr)
  45.         SysBeep(1);
  46.         
  47. } // DoFindInMovie
  48.  
  49. // --------------------------------------------------------------------------------------------------------------
  50.  
  51. static OSErr    MovieAdjustMenus(WindowRef pWindow, WindowDataPtr pData)
  52. {
  53. #pragma unused (pWindow)
  54.  
  55.     if (GetMovieIndTrackType( ((MovieDataPtr)pData)->theMovie, 1, 'text', movieTrackCharacteristic) != nil)
  56.         {
  57.         EnableCommand(cFind);
  58.         if (gFindString[0] != 0)
  59.             EnableCommand(cFindAgain);
  60.         }
  61.  
  62.     return noErr;
  63.     
  64. } // MovieAdjustMenus
  65.  
  66. // --------------------------------------------------------------------------------------------------------------
  67.  
  68. static OSErr    MovieCommand(WindowRef pWindow, WindowDataPtr pData, short commandID, long menuResult)
  69. {
  70. #pragma unused (pWindow, menuResult)
  71.  
  72.     OSErr    anErr = noErr;
  73.     
  74.     switch (commandID)
  75.         {
  76.         case cFind:
  77.             if (ConductFindOrReplaceDialog(kFindWindowID) == cancel)
  78.                 break;
  79.         
  80.         case cFindAgain:
  81.             DoFindInMovie(((MovieDataPtr)pData)->theMovie, 
  82.                         &((MovieDataPtr)pData)->searchOffset,
  83.                         ((gEvent.modifiers & shiftKey) != 0));
  84.             break;
  85.         }
  86.     
  87.     return(anErr);
  88.     
  89. } // MovieCommand
  90.  
  91. // --------------------------------------------------------------------------------------------------------------
  92.  
  93. static OSErr    MovieCloseWindow(WindowRef pWindow, WindowDataPtr pData)
  94. {
  95. #pragma unused (pWindow)
  96.  
  97.     DisposeMovieController( ((MovieDataPtr)pData)->thePlayer);
  98.     DisposeMovie( ((MovieDataPtr)pData)->theMovie);
  99.     CloseMovieFile(pData->resRefNum);
  100.     pData->resRefNum = -1;
  101.     
  102.     return(noErr);
  103.     
  104. } // MovieCloseWindow
  105.  
  106. // --------------------------------------------------------------------------------------------------------------
  107. static OSErr MovieAdjustCursor(WindowRef pWindow, WindowDataPtr pData, Point *localMouse, Rect *globalRect)
  108. {
  109. #pragma unused (pWindow, pData, globalRect, localMouse)
  110.     
  111.     return(eActionAlreadyHandled);
  112.     
  113. } // MovieAdjustCursor
  114.  
  115. // --------------------------------------------------------------------------------------------------------------
  116.  
  117. static OSErr    MovieGetBalloon(WindowRef pWindow, WindowDataPtr pData, 
  118.         Point *localMouse, short * returnedBalloonIndex, Rect *returnedRectangle)
  119. {
  120. #pragma unused (pWindow, pData, localMouse, returnedRectangle)
  121.  
  122.     *returnedBalloonIndex = iDidTheBalloon;
  123.     
  124.     return(noErr);
  125.     
  126. } // MovieGetBalloon
  127.  
  128. // --------------------------------------------------------------------------------------------------------------
  129.  
  130. static Boolean    MovieFilterEvent(WindowRef pWindow, WindowDataPtr pData, EventRecord *pEvent)
  131. {
  132. #pragma unused (pWindow)
  133.     
  134.     return(MCIsPlayerEvent( ((MovieDataPtr)pData)->thePlayer, pEvent));
  135.     
  136. } // MovieFilterEvent
  137.  
  138. // --------------------------------------------------------------------------------------------------------------
  139.  
  140. static long MovieCalculateIdleTime(WindowRef pWindow, WindowDataPtr pData)
  141. {
  142. #pragma unused (pWindow, pData)
  143.  
  144.     if (!IsMovieDone( ((MovieDataPtr)pData)->theMovie))
  145.         return(0);
  146.     else
  147.         return(0x7FFFFFFF);
  148.         
  149. } // MovieCalculateIdleTime
  150.  
  151. // --------------------------------------------------------------------------------------------------------------
  152.  
  153. static OSErr    MovieMakeWindow(WindowRef pWindow, WindowDataPtr pData)
  154. {
  155.     OSErr                anErr;
  156.     short                actualResId;
  157.     Movie                theMovie;
  158.     MovieController        thePlayer;
  159.     Rect                movieBounds;
  160.     long                version;
  161.     
  162.     Gestalt(gestaltQuickTime, &version);
  163.     
  164.     pData->pAdjustMenus            = (AdjustMenusProc)            MovieAdjustMenus;
  165.     pData->pCommand                = (CommandProc)                MovieCommand;
  166.     pData->pCloseWindow         = (CloseWindowProc)            MovieCloseWindow;
  167.     pData->pFilterEvent         = (FilterEventProc)            MovieFilterEvent;
  168.     pData->pGetBalloon             = (GetBalloonProc)            MovieGetBalloon;
  169.     pData->pCalculateIdleTime    = (CalculateIdleTimeProc)    MovieCalculateIdleTime;
  170.     pData->pAdjustCursor        = (AdjustCursorProc)        MovieAdjustCursor;
  171.     pData->dragWindowAligned    = (version >= 0x01508000);
  172.     
  173.     actualResId = DoTheRightThing;
  174.  
  175.     // close down other paths to the file -- because we don't need them
  176.     if (pData->resRefNum != -1)
  177.         {
  178.         CloseResFile(pData->resRefNum);
  179.         pData->resRefNum = -1;
  180.         }
  181.     if (pData->dataRefNum != -1)
  182.         {
  183.         FSClose(pData->dataRefNum);
  184.         pData->dataRefNum = -1;
  185.         }
  186.  
  187.     anErr = OpenMovieFile(&pData->fileSpec, &pData->resRefNum, 0);
  188.     if (anErr == noErr)
  189.         {
  190.         anErr = NewMovieFromFile(&theMovie, pData->resRefNum, &actualResId, (unsigned char *) 0, newMovieActive, (Boolean *) 0);
  191.         if (anErr == noErr)
  192.             anErr = GetMoviesError();
  193.         nrequire(anErr, NewMovieFromFile);
  194.         }
  195.     
  196.     // position the movie
  197.     GetMovieBox(theMovie, &movieBounds);
  198.     OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
  199.     SetMovieBox(theMovie, &movieBounds);
  200.     OffsetRect(&movieBounds, pData->contentRect.left, pData->contentRect.top);
  201.  
  202.     // make it draw in the correct window
  203.     SetMovieGWorld(theMovie, (CGrafPtr) pWindow, 0);
  204.     thePlayer = NewMovieController(theMovie, &movieBounds, mcTopLeftMovie);
  205.     MCGetControllerBoundsRect(thePlayer, &movieBounds);
  206.  
  207.     // make sure the window is the proper size
  208.     SizeWindow(pWindow, movieBounds.right - movieBounds.left,
  209.                             movieBounds.bottom - movieBounds.top, false);
  210.     pData->contentRect.right = pData->contentRect.left + 
  211.         movieBounds.right - movieBounds.left;
  212.     pData->contentRect.bottom = pData->contentRect.top + 
  213.         movieBounds.bottom - movieBounds.top;
  214.     if (pData->dragWindowAligned)
  215.         AlignWindow((WindowPtr)pWindow, false, nil, nil);
  216.     
  217.     // enable keyboard events
  218.     MCDoAction(thePlayer, mcActionSetKeysEnabled, (void*)1);
  219.     
  220.     // Save a reference to the movie
  221.     ((MovieDataPtr)pData)->theMovie = theMovie;
  222.     ((MovieDataPtr)pData)->thePlayer = thePlayer;
  223.     ((MovieDataPtr)pData)->searchOffset = 0;
  224.     
  225. // FALL THROUGH EXCEPTION HANDLING
  226. NewMovieFromFile:
  227.     return(anErr);
  228.     
  229. } // MovieMakeWindow
  230.  
  231.  
  232. // --------------------------------------------------------------------------------------------------------------
  233.  
  234. OSErr    MoviePreflightWindow(PreflightPtr pPreflightData)
  235. {    
  236.     pPreflightData->continueWithOpen     = true;
  237.     pPreflightData->makeProcPtr         = MovieMakeWindow;
  238.     pPreflightData->resourceID            = kMovieWindowID;
  239.     pPreflightData->storageSize         = sizeof(MovieDataRecord);
  240.  
  241.     return(noErr);
  242.     
  243. } // MoviePreflightWindow
  244.  
  245. // --------------------------------------------------------------------------------------------------------------
  246.  
  247. void MovieGetFileTypes(OSType * pFileTypes, OSType * pDocumentTypes, short * numTypes)
  248. {
  249.     if (gMachineInfo.haveQuickTime)
  250.         {
  251.         pFileTypes[*numTypes]         = 'MooV';
  252.         pDocumentTypes[*numTypes]     = kMovieWindow;
  253.         (*numTypes)++;
  254.         }
  255.         
  256. } // MovieGetFileTypes
  257.